// 获取网卡ip地址

package util

import (
	"bytes"
	"fmt"
	"log"
	"os/exec"
	"strings"
)

func FetchIP(nic string) string {
	cmdLine := fmt.Sprintf("ifconfig %s|grep -Po '(?<=inet ).*(?=netmask)'", nic)
	cmd := exec.Command("sh", "-c", cmdLine)
	var ret bytes.Buffer
	cmd.Stdout = &ret
	err := cmd.Run()
	if err != nil {
		log.Fatal("fetch local ip address failed: ", err.Error())
	}
	return strings.TrimSpace(ret.String())
}
